package GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import Other.Glob;
import database.DatabaseExtension;
import database.MyAddressee;
import database.MyCountry;
import database.MyPayment;
import database.MySupplier;
import database.MyTax;
import database.NewTirDataBase;
import enums.DialogType;
import enums.SearchType;

public class Supplier extends JFrame {
	

	//variable for singleton
		private static Supplier supplierInstance = null;
		private JPanel contentPane;
		private JTextField tfCode;
		private JTextField tfName;
		//variable that represent this window frame
		private JFrame thisFrame = this;
		//variable to enable and disable the screen
		boolean disable;
		private JTextField tfAddress;
		private JTextField tfPostcode;
		private JTextField tfTown;
		private JTextField tfProvince;
		private JTextField tfEmail;
		private JTextField tfPaymentName;
		private JTextField tfNif;
		private JTextField tfFax;
		private JTextField tfPhone;
		private JTextField tfCountryCode;
		private JTextField tfCountryName;
		private JTextField tfPaymentCode;
		private JTextField tfTaxCode;
		private JTextField tfTaxName;
		private JTextField tfMaturity;
		
		private Supplier() {
			//set the title
			setTitle("Supplier");
			setVisible(true);
			//set resizable
			setResizable(false);
			setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			setBounds(100, 100, 856, 444);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			JPanel leftPanel = new JPanel();
			leftPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			
			JPanel rightPanel = new JPanel();
			rightPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			GroupLayout gl_contentPane = new GroupLayout(contentPane);
			gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_contentPane.createSequentialGroup()
								.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 412, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(rightPanel, GroupLayout.PREFERRED_SIZE, 421, GroupLayout.PREFERRED_SIZE))
							.addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE))
						.addContainerGap())
			);
			gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.TRAILING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
							.addComponent(rightPanel, 0, 0, Short.MAX_VALUE)
							.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 314, Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(5))
			);
			
			JLabel lblEmail = new JLabel("Email");
			
			tfEmail = new JTextField();
			tfEmail.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfCode.requestFocus();				
					}
				}
			});
			tfEmail.setColumns(10);
			
			tfPaymentName = new JTextField();
			tfPaymentName.setEditable(false);
			tfPaymentName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfEmail.requestFocus();				
					}
				}
			});
			tfPaymentName.setColumns(10);
			
			JLabel lblContact = new JLabel("Payment");
			
			JLabel lblNifcif = new JLabel("Nif,Cif");
			
			tfNif = new JTextField();
			tfNif.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfPaymentCode.requestFocus();				
					}
				}
			});
			tfNif.setColumns(10);
			
			tfFax = new JTextField();
			tfFax.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfNif.requestFocus();				
					}
				}
			});
			tfFax.setColumns(10);
			
			JLabel lblFax = new JLabel("Fax");
			
			JLabel label_4 = new JLabel("Phone");
			
			tfPhone = new JTextField();
			tfPhone.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfFax.requestFocus();				
					}
				}
			});
			tfPhone.setColumns(10);
			
			tfCountryCode = new JTextField();
			tfCountryCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						//enterDataSecondary(tfCountryCode, tfCountryName, SearchType.COUNTRY);
						ScreenHandling.enterData(tfCountryCode, tfCountryName, tfPhone, SearchType.COUNTRY);
						System.out.println("button is being pressed");
						
					}
				}
			});
			tfCountryCode.setColumns(10);
			
			JLabel lblCountry = new JLabel("Country");
			
			JButton sbCountry = new JButton("F5 Search");
			
			sbCountry.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.SUPPLIERCOUNTRY;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			
			tfCountryName = new JTextField();
			tfCountryName.setEditable(false);
			tfCountryName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						
							
					}
				}
			});
			tfCountryName.setColumns(10);
			
			tfPaymentCode = new JTextField();
			tfPaymentCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						//enterDataSecondary(tfPaymentCode, tfPaymentName, SearchType.PAYMENT);
						ScreenHandling.enterData(tfPaymentCode, tfPaymentName, tfTaxCode, SearchType.PAYMENT);
						System.out.println("button is being pressed");
						
					}
				}
			});
			tfPaymentCode.getInputMap(tfPaymentCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfPaymentCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 
					 //when textcode focused and f5 pressed search dialog comes up
					 Glob.type= SearchType.SUPPLIERPAYMENT;
					 
					 search();
					
				    }
			});
			tfPaymentCode.setColumns(10);
			
			JButton sbPayment = new JButton("F5 Search");
			sbPayment.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.SUPPLIERPAYMENT;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			
			JLabel lblNewLabel = new JLabel("Tax");
			
			tfTaxCode = new JTextField();
			tfTaxCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						//enterDataSecondary(tfTaxCode, tfTaxName, SearchType.TAX);
						ScreenHandling.enterData(tfTaxCode, tfTaxName, tfEmail, SearchType.TAX);
						System.out.println("button is being pressed");
						
					}
				}
			});
			tfTaxCode.getInputMap(tfTaxCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfTaxCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 
					 //when textcode focused and f5 pressed search dialog comes up
					 Glob.type= SearchType.SUPPLIERTAX;
					 
					 search();
					
				    }
			});
			tfTaxCode.setColumns(10);
			
			JButton sbTax = new JButton("F5 Search");
			
			sbTax.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.SUPPLIERTAX;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			
			tfTaxName = new JTextField();
			tfTaxName.setEditable(false);
			tfTaxName.setColumns(10);
			
			GroupLayout gl_rightPanel = new GroupLayout(rightPanel);
			gl_rightPanel.setHorizontalGroup(
				gl_rightPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_rightPanel.createSequentialGroup()
						.addGap(41)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(lblEmail, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
							.addGroup(Alignment.TRAILING, gl_rightPanel.createSequentialGroup()
								.addComponent(lblCountry, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(tfCountryCode, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addComponent(sbCountry))
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING, false)
									.addComponent(lblFax, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
									.addComponent(lblNewLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
									.addComponent(lblNifcif, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
									.addComponent(lblContact, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)))
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
											.addGroup(gl_rightPanel.createSequentialGroup()
												.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(ComponentPlacement.RELATED, 144, Short.MAX_VALUE))
											.addGroup(gl_rightPanel.createSequentialGroup()
												.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING, false)
													.addGroup(gl_rightPanel.createSequentialGroup()
														.addComponent(tfPaymentCode, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(tfPaymentName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
													.addGroup(gl_rightPanel.createSequentialGroup()
														.addComponent(tfTaxCode, 0, 0, Short.MAX_VALUE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(tfTaxName, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED))
													.addGroup(gl_rightPanel.createSequentialGroup()
														.addComponent(tfEmail)
														.addPreferredGap(ComponentPlacement.RELATED)))
												.addGap(18)
												.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
													.addComponent(sbTax)
													.addComponent(sbPayment, GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))))
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING, false)
											.addComponent(tfFax)
											.addComponent(tfPhone, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
										.addPreferredGap(ComponentPlacement.RELATED, 148, Short.MAX_VALUE)))))
						.addGap(30))
			);
			gl_rightPanel.setVerticalGroup(
				gl_rightPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_rightPanel.createSequentialGroup()
						.addGap(39)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(1)
										.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
											.addComponent(tfCountryCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(4)
										.addComponent(lblCountry)))
								.addGap(23)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
									.addComponent(label_4)
									.addComponent(tfPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGap(23)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
									.addComponent(lblFax)
									.addComponent(tfFax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGap(18)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(3)
										.addComponent(lblNifcif))
									.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addComponent(sbCountry))
						.addGap(18)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblContact)
							.addComponent(tfPaymentCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(tfPaymentName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(sbPayment))
						.addGap(18)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(sbTax)
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel)
								.addComponent(tfTaxCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(tfTaxName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGap(14)
								.addComponent(lblEmail))
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(tfEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGap(23))
			);
			rightPanel.setLayout(gl_rightPanel);
			JButton btnSave = new JButton("F4 Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//Save when button pressed
					Glob.type = SearchType.SUPPLIER;
					try {
						saveDialog();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			tfCode = new JTextField();
			tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "Save");
			tfCode.getActionMap().put("Save", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					//Save when button pressed
					 Glob.type = SearchType.SUPPLIER;
					 try {
							saveDialog();
						} catch (Exception b) {
							// TODO Auto-generated catch block
							b.printStackTrace();
						}
				 }
			});
			JButton btnRemove = new JButton("F6 Remove");
			//set up F6 for removing
			btnRemove.getInputMap(btnRemove.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
			btnRemove.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 //remove item when F6 button pressed
					ScreenHandling.remove(tfCode, SearchType.SUPPLIER);
				    }
			});
			btnRemove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//remove item when button pressed
					ScreenHandling.remove(tfCode, SearchType.SUPPLIER);
				}
			});
			JButton btnFReset = new JButton("F7 Reset");
			//set up F7 for reset
			btnFReset.getInputMap(btnFReset.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
			btnFReset.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					//reset field when button pressed
					 resetFields();
				    }
			});
			btnFReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//reset field when button pressed
					resetFields();
				}
			});
			JButton btnEscExit = new JButton("Esc Exit");
			//set up Escape for exit
			btnEscExit.getInputMap(btnEscExit.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
			btnEscExit.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					if (disable == false){
					//escape from the screen
						escape();
					}
				    }
			});
			btnEscExit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//if the screen is disable it is possible to escape
					if (disable == false){
						escape();
					}
				}
			});
			GroupLayout gl_buttonPanel = new GroupLayout(buttonPanel);
			gl_buttonPanel.setHorizontalGroup(
				gl_buttonPanel.createParallelGroup(Alignment.TRAILING)
					.addGroup(gl_buttonPanel.createSequentialGroup()
						.addGap(18)
						.addComponent(btnRemove)
						.addPreferredGap(ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
						.addComponent(btnEscExit)
						.addGap(29)
						.addComponent(btnFReset)
						.addGap(34)
						.addComponent(btnSave)
						.addGap(18))
			);
			gl_buttonPanel.setVerticalGroup(
				gl_buttonPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_buttonPanel.createSequentialGroup()
						.addGap(21)
						.addGroup(gl_buttonPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnSave)
							.addComponent(btnRemove)
							.addComponent(btnFReset)
							.addComponent(btnEscExit))
						.addContainerGap(38, Short.MAX_VALUE))
			);
			buttonPanel.setLayout(gl_buttonPanel);
			JLabel lblCode = new JLabel("Code");
			JLabel lblName = new JLabel("Name");
			//set up F5 for saving only when the text is focused
			tfCode.getInputMap(tfCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 Glob.type = SearchType.SUPPLIER;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				    }
			});
			JButton sbCode = new JButton("F5 Search");
			sbCode.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.SUPPLIER;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			tfCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a number is entered with ENTER
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						
						enterData();
						
						//tfName.requestFocus();
					}
					
				}
			});
			tfCode.setColumns(10);
			tfName = new JTextField();
			tfName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a number is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfAddress.requestFocus();	
					}
				}
			});
			tfName.setColumns(10);
			
			JLabel lblAddress = new JLabel("Address");
			
			tfAddress = new JTextField();
			tfAddress.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfPostcode.requestFocus();				
					}
				}
			});
			
			tfAddress.setColumns(10);
			
			JLabel lblPostcode = new JLabel("Postcode");
			
			tfPostcode = new JTextField();
			tfPostcode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfTown.requestFocus();				
					}
				}
			});
			tfPostcode.setColumns(10);
			
			JLabel lblTown = new JLabel("Town");
			
			tfTown = new JTextField();
			tfTown.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfProvince.requestFocus();				
					}
				}
			});
			tfTown.setColumns(10);
			
			JLabel lblProvince = new JLabel("Province");
			
			tfProvince = new JTextField();
			tfProvince.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfMaturity.requestFocus();				
					}
				}
			});
			//set up F5 for searching only when the text is focused
			tfCountryCode.getInputMap(tfCountryCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfCountryCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 
					 //when textcode focused and f5 pressed search dialog comes up
					 Glob.type= SearchType.SUPPLIERCOUNTRY;
					 
					search();
					
				    }
			});
			
			
			tfMaturity = new JTextField();
			tfMaturity.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfCountryCode.requestFocus();	
						System.out.println("Pressing maturity field");
					}
				}
			});
			tfProvince.setColumns(10);
			
			JLabel lblMaturity = new JLabel("Maturity");
			
		
			tfMaturity.setColumns(10);
			GroupLayout gl_leftPanel = new GroupLayout(leftPanel);
			gl_leftPanel.setHorizontalGroup(
				gl_leftPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_leftPanel.createSequentialGroup()
						.addGap(42)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(lblCode, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblName)
							.addComponent(lblAddress)
							.addComponent(lblPostcode)
							.addComponent(lblTown)
							.addComponent(lblProvince)
							.addComponent(lblMaturity))
						.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING, false)
							.addComponent(tfName)
							.addGroup(gl_leftPanel.createSequentialGroup()
								.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
									.addComponent(tfCode, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
									.addComponent(tfPostcode)
									.addComponent(tfTown)
									.addComponent(tfProvince)
									.addComponent(tfMaturity, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(sbCode, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
							.addComponent(tfAddress))
						.addGap(47))
			);
			gl_leftPanel.setVerticalGroup(
				gl_leftPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_leftPanel.createSequentialGroup()
						.addGap(37)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblCode)
							.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(sbCode))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblName)
							.addComponent(tfName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblAddress)
							.addComponent(tfAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblPostcode)
							.addComponent(tfPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblTown)
							.addComponent(tfTown, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblProvince)
							.addComponent(tfProvince, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblMaturity)
							.addComponent(tfMaturity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(30))
			);
			//request focus when object is called
			tfCode.requestFocus(true);
			leftPanel.setLayout(gl_leftPanel);
			contentPane.setLayout(gl_contentPane);	
		}
	//singleton
	public static Supplier getInstance(){
		if(supplierInstance == null){
			supplierInstance = new Supplier();
		}
		return supplierInstance;
		}
	//initiate data
	public void init(MySupplier supplier){
		
	
		
		tfCode.setText(supplier.getSupplierID());	
		tfName.setText(supplier.getName());	
		tfAddress.setText(supplier.getAddress());
		tfPostcode.setText(supplier.getPostcode());
		tfTown.setText(supplier.getTown());
		tfProvince.setText(supplier.getProvince());
		tfMaturity.setText(supplier.getMaturity());
		if(String.valueOf(supplier.getCountryCodeID()).equals("null")){
			
			tfCountryCode.setText("");
			
		}else{
		tfCountryCode.setText(supplier.getCountryCodeID());
		}
		tfCountryName.setText(supplier.getCountryName());
		tfPhone.setText(supplier.getPhoneNumber());
		tfFax.setText(supplier.getFax());
		tfNif.setText(supplier.getNif());
		if(String.valueOf(supplier.getPaymentCodeID()).equals("null")){
			
			tfPaymentCode.setText("");
			
		}else{
			tfPaymentCode.setText(supplier.getPaymentCodeID());
		}
		tfPaymentName.setText(supplier.getPaymentName());
		if(String.valueOf(supplier.getTaxCodeID()).equals("null")){
			
			tfTaxCode.setText("");
			
		}else{
			tfTaxCode.setText(supplier.getTaxCodeID());
		}
		
		tfTaxName.setText(supplier.getTaxName());
		tfEmail.setText(supplier.getEmail());
		
	}
	
	public void initCountry(MyCountry country){
		
		tfCountryCode.setText(country.getCountryID());
		tfCountryName.setText(country.getCountryName());
		
	}
	
	public void initPayment(MyPayment payment){
		
		tfPaymentCode.setText(payment.getPaymentID());
		tfPaymentName.setText(payment.getPaymentName());
		
	}
	
public void initTax(MyTax tax){
		
		tfTaxCode.setText(tax.getTaxID());
		tfTaxName.setText(tax.getTaxName());
		
	}
	
	
	//method needed to set focus from other screens
	public void setTextCodeFocus(){
		tfCode.requestFocus();
	}
	
	//reset fields
	public void resetFields(){
		tfCode.setText("");
		tfName.setText("");
		tfAddress.setText("");
		tfPostcode.setText("");
		tfTown.setText("");
		tfProvince.setText("");
		tfMaturity.setText("");
		tfCountryCode.setText("");
		tfCountryName.setText("");
		tfPhone.setText("");
		tfFax.setText("");
		tfNif.setText("");
		tfPaymentCode.setText("");
		tfPaymentName.setText("");
		tfTaxCode.setText("");
		tfTaxName.setText("");
		tfEmail.setText("");
		tfCode.requestFocus();
	}
	//method needed to get value from text code in other screens
	public String getFieldCode() {
	    return tfCode.getText();
	 }
	//method needed to get value from text Country field in other screens
	public String getFieldCountry() {
		return tfCountryCode.getText();
	}
	//method needed to get value from text Country field in other screens
		public String getFieldPayment() {
			return tfPaymentCode.getText();
		}
		
		//method needed to get value from text Country field in other screens
		public String getFieldTax() {
			return tfTaxCode.getText();
		}
	//method for saving the object
	public void saveDialog() throws Exception{
		//Glob.type = SearchType.TAX;
		Glob.setDialog(DialogType.OVERWRITE);
		try{
		if(tfName.getText().trim().length()<1) {
			JOptionPane.showMessageDialog(null, "Name may not be empty");
			tfName.requestFocus();
			return;
		}
if(!tfCode.getText().trim().isEmpty()) {
			
			if(Integer.parseInt(tfCode.getText())<1){
				JOptionPane.showMessageDialog(null, "Code has to be a valid number");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
				
			}
			}
		
		ScreenHandling.checkIdOnTable(tfCountryCode, SearchType.COUNTRY);
		
		
		ScreenHandling.checkIdOnTable(tfPaymentCode, SearchType.PAYMENT);
		

		
		ScreenHandling.checkIdOnTable(tfTaxCode, SearchType.TAX);
		

		
			
		ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from supplier");
		//System.out.println(ids);
		boolean Ok = false;
		if(tfCode.getText().trim().length()<1) {
			//Do nothing
			}else{
				
		for (int i: ids) {
			if (i == Integer.parseInt(tfCode.getText()))
					Ok=true;
		}		
			if (Ok) {
			//JOptionPane.showMessageDialog(null, "This ID is not on the Tax table");
			Dialog d = Dialog.getInstance();
			d.setVisible(true);
			Supplier.getInstance().disableSupplier();
			//tfCode.requestFocus();
			return;
			
		}
			}
			if(Ok==false){
			save();			
			}
			
	}catch (NumberFormatException e){
		System.out.println("This is the general expetion inside saveDialog");
		JOptionPane.showMessageDialog(null, "Code must be a positive number");
		tfCode.setText("");
		tfCode.requestFocus();
		e.printStackTrace();
		return;
	}
	}
	//method that saves the object in the database
	public void save(){
		
		Glob.setType(SearchType.SUPPLIER);
		
		String[] data = new String[14];	
		
		try{
		data[0] = tfCode.getText();	
		}catch(Exception code){
			
			code.printStackTrace();
			
		}
		System.out.println("This is the id got from the tfcode inside save" + data[0]);
		if(data[0].isEmpty()){
		data[0] = null;
		}
	data[1] = tfName.getText();
	
	data[2] = tfAddress.getText();
	
	data[3] = tfPostcode.getText();
	
	data[4] = tfTown.getText();
	
	data[5] = tfProvince.getText();
	
	data[6] = tfMaturity.getText();
	
	System.out.println("This is the length of the country code in the save method "+ tfCountryCode.getText().trim().length());
	
	try{
		if(tfCountryCode.getText().isEmpty()){
			
			data[7] = null;
			
		}else{
			//System.out.println("The ELSE has happeneded");
			data[7] = tfCountryCode.getText();
		}

	}catch(Exception country){
		country.printStackTrace();
		JOptionPane.showMessageDialog(null, "Country must be a proper integer number");
	}
	
	
	//data[7] = tfCountryName.getText();
	//we don't need data seven for saving, just for populating
	
	data[8] = tfPhone.getText();

	data[9] = tfFax.getText();
	
	data[10] = tfNif.getText();
	
	try{
		if(tfPaymentCode.getText().isEmpty()){
			
			data[11] = null;
			
		}else{
			//System.out.println("The ELSE has happeneded");
			data[11] = tfPaymentCode.getText();
		}

	}catch(Exception payment){
		payment.printStackTrace();
		JOptionPane.showMessageDialog(null, "Payment must be a proper integer number");
	}
	
	try{
		if(tfTaxCode.getText().isEmpty()){
			
			data[12] = null;
			
		}else{
			//System.out.println("The ELSE has happeneded");
			data[12] = tfTaxCode.getText();
		}

	}catch(Exception tax){
		tax.printStackTrace();
		JOptionPane.showMessageDialog(null, "Tax must be a proper integer number");
	}
	
	data[13] = tfEmail.getText();
	
	//System.out.println("This is the data String" + data[0]+data[1]+data[2]+data[3]+data[4]+data[5]+data[6]+data[7]+data[8]+data[9]+data[10]+data[11]+data[12]);
	
	//System.out.println("this is the string of data from Addressee in the method save" + data[0] + data[1] + data[2]);
		MySupplier supplier = new MySupplier(data);
		//System.out.println("this is the data inside de object in the method save" + Tax.getTaxID() + Tax.getTaxName());
		try{
			DatabaseExtension.saveSupplier(supplier);
		}catch (Exception saveSupplier){
			
			saveSupplier.printStackTrace();
			System.out.println("this is the exception released in saving an addressee");
			
			
		}
			resetFields();
			setTextCodeFocus();

	}
	//method that calls the searchwindow
	public void search(){
	
		SearchWindow s = SearchWindow.getInstance();
		s.setTitle();
		s.setVisible(true);
		disableSupplier();
	}
	//method to disable the screen
	public void disableSupplier(){
		Supplier.getInstance().disable();
		disable = true;
	}
	//method to enable the screen
	public void enableSupplier(){
		Supplier.getInstance().enable();
		disable = false;
		if (Glob.type ==  SearchType.SUPPLIER){
			tfCode.requestFocus();
		}else if (Glob.type ==  SearchType.SUPPLIERCOUNTRY){
			tfCountryCode.requestFocus();
		}else if (Glob.type ==  SearchType.SUPPLIERPAYMENT){
			tfPaymentCode.requestFocus();
		}else if (Glob.type ==  SearchType.SUPPLIERTAX){
			tfTaxCode.requestFocus();
		}
		
		Supplier.getInstance().toFront();
	}
	//method to get the disable boolean
	public boolean getDisable(){
		return disable;
	}
	/**
	//method that calls the dialog screen to remove an item
	public void remove(){
		
		//set up global variable for this screen
		Glob.dialog= DialogType.DELETE;
		//set up global variable for deleting an item
		Glob.type = SearchType.SUPPLIER;
		System.out.println("This is the global dialog constant inside the remove method " + Glob.dialog);
		try{
			//if the value on the code textfield is empty
			if(Integer.valueOf(tfCode.getText()) <= 0) {
				JOptionPane.showMessageDialog(null, "Code may not be empty and must have a proper ID");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
			//getting all the ids from Tax table in the database
			ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from Supplier");
			System.out.println(ids);
			boolean ok = false;
			for (int i: ids) {
				if (i == Integer.valueOf(tfCode.getText()))
						ok=true;
			}
				//if the id is not on the table is not possible to remove it
				if (!ok) {
				JOptionPane.showMessageDialog(null, "This ID is not in the Supplier table");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
			//calling the dialog screen
			Dialog d = Dialog.getInstance();
			d.setVisible(true);
			//disable Tax screen
			disableSupplier();
			}catch(Exception z){
				//code textfield has to contain a proper value and not be empty
				JOptionPane.showMessageDialog(null, "Code may not be empty and must have a proper ID");
				tfCode.requestFocus();
				tfCode.setText(null);
				z.printStackTrace();
			}
	}
	*/
	//method that populates the screen fields with the object
	public void enterData(){
		try{
			//set up global variable for the Tax screen
			Glob.setType(SearchType.SUPPLIER);
			//if textfield is 0 or less than 0
			if(tfCode.getText().trim().length()<1){
				tfName.requestFocus();
				
			}
			else if(Integer.parseInt(tfCode.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
		//Select the row that will populate the screen
		NewTirDataBase s = (MySupplier) DatabaseExtension.getTableID("SELECT * FROM supplier WHERE supplierID = '" + tfCode.getText() +"'")[0];
		//NewTirDataBase a = (MyAddressee) DatabaseExtension.getTableID("Select AddresseeID, addressee.name , address, postcode, town, province, addressee.countryID, country.name, phonenumber, fax, nif, contact, email from addressee, country where addressee.countryid = country.countryID and addresseeID = '" + tfCode.getText() +"'")[0];
		
		//System.out.println(((MySupplier) s).getAddresseeID());
		//populating the fields
		tfName.setText(((MySupplier) s).getName());
		tfAddress.setText(((MySupplier) s).getAddress());
		tfPostcode.setText(((MySupplier) s).getPostcode());
		tfTown.setText(((MySupplier) s).getTown());
		tfProvince.setText(((MySupplier) s).getProvince());
		tfMaturity.setText(((MySupplier) s).getMaturity());
		try{
			//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
			if(String.valueOf(((MySupplier) s).getCountryCodeID()).equals("null")){
				
				tfCountryCode.setText("");
				
			}else{
			tfCountryCode.setText(((MySupplier) s).getCountryCodeID());
			}
		
			}catch(Exception e){
				System.out.println("This is the exception regarding the country code");
				e.printStackTrace();
			}
		
		tfCountryName.setText(((MySupplier) s).getCountryName());
		tfPhone.setText(((MySupplier) s).getPhoneNumber());
		tfFax.setText(((MySupplier) s).getFax());
		tfNif.setText(((MySupplier) s).getNif());
		
		try{
			//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
			if(String.valueOf(((MySupplier) s).getPaymentCodeID()).equals("null")){
				
				tfPaymentCode.setText("");
				
			}else{
			tfPaymentCode.setText(((MySupplier) s).getPaymentCodeID());
			}
		
			}catch(Exception e){
				System.out.println("This is the exception regarding the payment code");
				e.printStackTrace();
			}
		
		tfPaymentName.setText(((MySupplier) s).getPaymentName());
		
		try{
			
			if(String.valueOf(((MySupplier) s).getTaxCodeID()).equals("null")){
				
				tfTaxCode.setText("");
				
			}else{
			tfTaxCode.setText(((MySupplier) s).getTaxCodeID());
			}
		
			}catch(Exception e){
				System.out.println("This is the exception regarding the tax code");
				e.printStackTrace();
			}
		
		
		
		tfTaxName.setText(((MySupplier) s).getTaxName());
		
		
		tfEmail.setText(((MySupplier) s).getEmail());
		
		
		//System.out.println("this is the Tax id and name" + c.getTaxID() + c.getTaxName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		tfName.requestFocus();
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to empty
			//tfName.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			tfName.requestFocus();
		}catch(NumberFormatException exception){
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			tfCode.setText(null);
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	//method to close the screen
	public void escape(){
		Supplier.getInstance().dispose();
		Menu.getInstance().enableMenu();
	}
	

/**
	public void enterDataSecondary(JTextField code, JTextField name, SearchType type){
		try{
			//set up global variable for the country screen
			Glob.type = type;
			//if textfield is 0 or less than 0
			if(Integer.valueOf(code.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.setText("");
				code.requestFocus();
				return;
			}
		//Select the row that will populate the screen
		if (type == SearchType.COUNTRY){
		NewTirDataBase table = (MyCountry) DatabaseExtension.getTableID( "SELECT * FROM Country WHERE CountryID = '" + code.getText() +"'")[0];
		//populating the fields
		name.setText(((MyCountry) table).getCountryName());
		tfPhone.requestFocus();
		}else if (type == SearchType.PAYMENT){
			NewTirDataBase table = (MyPayment) DatabaseExtension.getTableID( "SELECT * FROM Payment WHERE PaymentID = '" + code.getText() +"'")[0];
			//populating the fields
			name.setText(((MyPayment) table).getPaymentName());
			tfTaxCode.requestFocus();
		}else if (type == SearchType.TAX){
			NewTirDataBase table = (MyTax) DatabaseExtension.getTableID( "SELECT * FROM Tax WHERE taxID = '" + code.getText() +"'")[0];
			//populating the fields
			name.setText(((MyTax) table).getTaxName());
			tfEmail.requestFocus();
		}
		
		//System.out.println("this is the country id and name" + c.getCountryID() + c.getCountryName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to emptym
			JOptionPane.showMessageDialog(null, "Code is not in the database");
			code.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			//tfCountryName.requestFocus();
		}catch(NumberFormatException exception){
			//if code tf is empty
			if(code.getText().isEmpty()){
				if (type == SearchType.COUNTRY){
					tfCountryName.setText("");
					tfPhone.requestFocus();
				}else if (type == SearchType.PAYMENT){
					tfPaymentName.setText("");
					tfTaxCode.requestFocus();
				}else if (type == SearchType.TAX){
					tfTaxName.setText("");
					tfEmail.requestFocus();
				}
				
				return;
			}else{
				//set code tf to null
				code.setText(null);
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.requestFocus();
			} 
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	*/
}
